#  _*_ coding: utf-8 _*_
from DjangoUeditor.models import UEditorField

from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db import models

# Create your models here.

class User_Info(models.Model):
    SEX_CHOICES = (
        ('m','男性'),
        ('f','女性'),
        ('s','未知'),
    )
    ROLE_CHOICES = (
        (90,'总版主'),
        (80,'版主'),
        (70,'荣誉会员'),
        (60,'会员'),
    )
    r_user = models.OneToOneField(User,verbose_name='用户引用')
    nickname = models.CharField(verbose_name='昵称',default='',blank=True,max_length=50)
    city = models.CharField(verbose_name='所在城市',default='',blank=True,max_length=20)
    sex = models.CharField(verbose_name='性别',max_length=1,choices=SEX_CHOICES,default='s')
    birthday = models.DateField(verbose_name='生日',default='1900-01-01',)
    constellation = models.CharField(verbose_name='星座',default='',blank=True,max_length=20)
    job = models.CharField(verbose_name='职业',default='',blank=True,max_length=20)
    signature = models.TextField(verbose_name='个性签名',default='',blank=True)
    # signature = UEditorField(verbose_name='个性签名',width=400, height=300, toolbars="mini",
    #                          imagePath="signature_img/%(basename)s_%(datetime)s.%(extname)s",
    #                          filePath="signature_file/%(basename)s_%(datetime)s.%(extname)s",
    #                          upload_settings={"imageMaxSize":1204000},
    #                          settings={},
    #                          command=None,
    #                          # event_handler=myEventHander(),
    #                          blank=True)
    i_code = models.CharField(verbose_name='邀请码',max_length=8,default='00000000')
    role = models.IntegerField(verbose_name='角色',choices=ROLE_CHOICES,default=60)
    grade = models.IntegerField(verbose_name='积分',default=0)
    portrait = models.FileField(verbose_name='头像',blank=True,null=True,upload_to='portrait')

    def __unicode__(self):
        return self.r_user.username

    def natural_key(self):
        return self.r_user.username

    # def get_absolute_url(self):
    #     return reverse('bbs/userinfo.html', kwargs={'pk': self.pk})

    class Meta:
        # ordering = ['r_user']
        verbose_name = '用户信息'
        verbose_name_plural = '用户信息'


class Message(models.Model):
    message_title = models.CharField(verbose_name='消息标题',max_length=50,default='',blank=True)
    message_content = models.TextField(verbose_name='消息内容',default='',blank=True)
    message_from = models.ForeignKey(User,verbose_name='发送者',related_name='message_from_user')
    message_to = models.ForeignKey(User,verbose_name='接受者',related_name='message_to_user')
    message_send_date = models.DateTimeField(verbose_name='发送日期',auto_now_add=True)
    message_read = models.BooleanField(verbose_name='已读',default=False)

    def __unicode__(self):
        return self.message_title

    def natural_key(self):
        return self.message_title

    class Meta:
        ordering = ['message_send_date']
        verbose_name = '站内信'
        verbose_name_plural = '站内信'



class Board(models.Model):
    board_title = models.CharField(verbose_name='版面标题',max_length=50,default='',blank=True)
    board_desc_short = models.CharField(verbose_name='版面短描述',max_length=50,default='',blank=True)
    board_desc = models.TextField(verbose_name='版面描述',default='',blank=True)
    board_total_topic = models.IntegerField(verbose_name='版面主题数',default='0')
    board_total_post = models.IntegerField(verbose_name='版面帖子数',default='0')
    board_last_update = models.DateTimeField(verbose_name='版面最后更新日期',default='1900-01-01')
    board_logo = models.FileField(verbose_name='版面图标',blank=True,null=True,upload_to='board_logo')

    def __unicode__(self):
        return self.board_title

    def natural_key(self):
        return self.board_title

    class Meta:
        # ordering = ['board_title']
        verbose_name = '版块'
        verbose_name_plural = '版块'


class Topic(models.Model):
    topic_title = models.CharField(verbose_name='主题标题',max_length=50,default='')
    topic_content = models.TextField(verbose_name='主题内容',default='',blank=True)
    topic_publisher = models.ForeignKey(User,verbose_name='主题发布者')
    topic_publish_date = models.DateTimeField(verbose_name='主题发布日期',auto_now_add=True)
    topic_last_update = models.DateTimeField(verbose_name='主题最后更新日期',auto_now=True)
    topic_board = models.ForeignKey(Board,verbose_name='从属版面')
    topic_top = models.BooleanField(verbose_name='置顶',default=False)
    topic_elite = models.BooleanField(verbose_name='精华帖',default=False)

    def __unicode__(self):
        return self.topic_title

    def natural_key(self):
        return self.topic_title

    class Meta:
        ordering = ['topic_publish_date']
        verbose_name = '主题'
        verbose_name_plural = '主题'

class Post(models.Model):
    post_content = models.TextField(verbose_name='帖子内容',default='',blank=True)
    post_publisher = models.ForeignKey(User,verbose_name='帖子发布者')
    post_publish_date = models.DateTimeField(verbose_name='帖子发布日期',auto_now_add=True)
    post_last_update = models.DateTimeField(verbose_name='帖子最后更新日期',auto_now=True)
    post_topic = models.ForeignKey(Topic,verbose_name='从属主题')
    post_board = models.ForeignKey(Board,verbose_name='从属版面')

    def __unicode__(self):
        return self.post_content

    def natural_key(self):
        return self.post_content

    class Meta:
        ordering = ['post_publish_date']
        verbose_name = '帖子'
        verbose_name_plural = '帖子'

class FPost(models.Model):
    fpost_content = models.TextField(verbose_name='跟帖内容',default='',blank=True)
    fpost_publisher = models.ForeignKey(User,verbose_name='跟帖发布者')
    fpost_publish_date = models.DateTimeField(verbose_name='跟帖发布日期',auto_now_add=True)
    fpost_last_update = models.DateTimeField(verbose_name='跟帖最后更新日期',auto_now=True)
    fpost_topic = models.ForeignKey(Topic,verbose_name='从属主题')
    fpost_board = models.ForeignKey(Board,verbose_name='从属版面')
    fpost_post = models.ForeignKey(Post,verbose_name='从属帖子')

    def __unicode__(self):
        return self.fpost_content
    def natural_key(self):
        return self.fpost_content

    class Meta:
        ordering = ['fpost_publish_date']
        verbose_name = '跟帖'
        verbose_name_plural = '跟帖'








